/*
 *	OpenSource code or fragments by agile webentwicklung, Marcus Fuhrmeister
 *
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions
 *	are met:
 *  
 *		* Redistribution of source code must retain the above OpenSource comment,
 *		this list of conditions and the following disclaimer.
 * 
 *		* Redistribution in binary form must reproduce the above OpenSource comment,
 *		this list of conditions and the following disclaimer in the
 *		documentation and/or other materials provided with the distribution.
 * 
 *	Neither the name of 'avocaod systems, Marcus Fuhrmeister' or the
 *	names of contributors may be used to endorse or promote products derived
 *	from this software without specific prior written permission.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 */
package de.fuhrmeister.browserchooser.view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.EventListener;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.event.ChangeListener;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * Options view class used in the settings dialog.
 * 
 *	@date 20.11.2008
 *	@author		Marcus Fuhrmeister
 */
public class OptionsView extends JPanel implements View {

	private static final long serialVersionUID = 5260980431614602207L;
	private JLabel sizeLabel;
	private JLabel sizeText;
	private JSlider sizeSlider;
	private JRadioButton northRadioButton;
	private JRadioButton westRadioButton;
	private AbstractButton eastRadioButton;
	private JRadioButton southRadioButton;
	private JLabel alwaysontopLabel;
	private JComboBox alwaysontopJComboBox;
	private JLabel languageLabel;
	private JComboBox languageJComboBox;
	private JLabel locationLabel;
	private JLabel colorLabel;
	private JTextField colorField;
	private JButton colorButton;
	private JButton resetButton;

	/**
	 * Constructor to create a new view.
	 * 
	 * @param width the width of the view
	 */
	public OptionsView(final int width) {
		super(new FlowLayout(FlowLayout.LEADING));
		final Border b = Borders.DIALOG_BORDER;
		final Insets i = b.getBorderInsets(this);
		final int colWidth = (width - (i.left + i.right)) / 4;
		final FormLayout layout = new FormLayout(colWidth + "px, " + colWidth + "px, " + colWidth + "px, " + colWidth + "px, " + colWidth + "px", "pref, 12px, pref, pref, pref, 12px, pref, 12px, pref, 12px, pref");
		this.setLayout(layout);
		this.setBorder(b);
		final CellConstraints cc = new CellConstraints();
		this.initComponents(cc);
	}

	/**
	 * Inits the components.
	 * 
	 * @param cc the CellConstraints
	 */
	private void initComponents(final CellConstraints cc) {
		alwaysontopLabel = new JLabel();
		this.add(alwaysontopLabel, cc.xy(1, 1));
		alwaysontopJComboBox = new JComboBox();
		this.add(alwaysontopJComboBox, cc.xy(2, 1));

		locationLabel = new JLabel();
		this.add(locationLabel, cc.xy(1, 4));
		northRadioButton = new JRadioButton();
		this.add(northRadioButton, cc.xy(3, 3));
		southRadioButton = new JRadioButton();
		this.add(southRadioButton, cc.xy(3, 5));
		westRadioButton = new JRadioButton();
		this.add(westRadioButton, cc.xy(2, 4));
		eastRadioButton = new JRadioButton();
		this.add(eastRadioButton, cc.xy(4, 4));
		final ButtonGroup locationButtonGroup = new ButtonGroup();
		locationButtonGroup.add(northRadioButton);
		locationButtonGroup.add(westRadioButton);
		locationButtonGroup.add(eastRadioButton);
		locationButtonGroup.add(southRadioButton);

		sizeLabel = new JLabel();
		this.add(sizeLabel, cc.xy(1, 7));
		sizeText = new JLabel();
		this.add(sizeText, cc.xy(2, 7));
		sizeSlider = new JSlider();
		sizeSlider.setMinorTickSpacing(8);
		sizeSlider.setMajorTickSpacing(16);
		sizeSlider.setPaintTicks(true);
		sizeSlider.setPaintLabels(true);
		sizeSlider.setSnapToTicks(true);
		this.add(sizeSlider, cc.xyw(3, 7, 2));

		languageLabel = new JLabel();
		this.add(languageLabel, cc.xy(1, 9));
		languageJComboBox = new JComboBox();
		this.add(languageJComboBox, cc.xyw(2, 9, 2));
		
		colorLabel = new JLabel();
		this.add(colorLabel, cc.xy(1, 11));
		colorField = new JTextField();
		colorField.setEditable(false);
		this.add(colorField, cc.xy(2, 11));
		colorButton = new JButton();
		this.add(colorButton, cc.xy(3, 11));
		resetButton = new JButton();
		this.add(resetButton, cc.xy(4, 11));
	}

	/**
	 * Sets the "icon size" components.
	 * 
	 * @param label the "icon size" label value
	 * @param value  the "icon size" value
	 * @param minValue the "icon size" minimum value
	 * @param maxValue the "icon size" maximum value
	 */
	public void setIconsizeProperties(final String label, final String value, final String minValue, final String maxValue) {
		sizeLabel.setText(" " + label);
		sizeText.setText(value);
		sizeSlider.setValue(Integer.parseInt(value));
		sizeSlider.setMinimum(Integer.parseInt(minValue));
		sizeSlider.setMaximum(Integer.parseInt(maxValue));
	}

	/**
	 * Sets the "icon size" value.
	 * 
	 * @param value the "icon size" value.
	 */
	public void setIconsizeText(final String value) {
		sizeText.setText(value);
	}

	/**
	 * Sets the "location" components values.
	 * 
	 * @param label the location label value
	 * @param value the location value
	 * @param label1 the north location value
	 * @param label2 the south location value
	 * @param label3 the west location value
	 * @param label4 the east location value
	 */
	public void setLocationProperties(final String label, final String value, final String label1, final String label2, final String label3, final String label4) {
		locationLabel.setText(label);
		northRadioButton.setText(label1);
		southRadioButton.setText(label2);
		westRadioButton.setText(label3);
		eastRadioButton.setText(label4);
		if (value.equalsIgnoreCase(label1)) {
			northRadioButton.setSelected(true);
		}
		if (value.equalsIgnoreCase(label2)) {
			southRadioButton.setSelected(true);
		}
		if (value.equalsIgnoreCase(label3)) {
			westRadioButton.setSelected(true);
		}
		if (value.equalsIgnoreCase(label4)) {
			eastRadioButton.setSelected(true);
		}
	}

	/**
	 * Sets the "always on top" components.
	 * 
	 * @param label the "always on top" label
	 * @param value the selected value
	 * @param minValue the minimum value
	 * @param maxValue the maximum value
	 */
	public void setAlwaysontopProperties(final String label, final String value, final String minValue, final String maxValue) {
		alwaysontopLabel.setText(label);
		alwaysontopJComboBox.setModel(new DefaultComboBoxModel(new String[] { minValue, maxValue }));
		alwaysontopJComboBox.setSelectedItem(value);
		alwaysontopJComboBox.setActionCommand(label);
	}

	/**
	 * Sets the "language" components.
	 * 
	 * @param label the "language" label value
	 * @param value the selected value
	 * @param languages the selectable values
	 */
	public void setLanguageProperties(final String label, final String value, final String[] languages) {
		languageLabel.setText(label);
		languageJComboBox.setModel(new DefaultComboBoxModel(languages));
		languageJComboBox.setSelectedItem(value);
		languageJComboBox.setActionCommand(label);
	}
	
	public void setColorProperties(final String label, final Color color, final String value, final String text, final String reset) {
		if (label != null) colorLabel.setText(label);
		if (value != null) { colorField.setText(value); colorField.setBackground(color); }
		if (text != null)  { colorButton.setText(text); colorButton.setActionCommand(text);}
		if (text != null)  { resetButton.setText(reset); resetButton.setActionCommand(reset);}
	}

	/**
	 * Registers the listeners.
	 */
	public void registerListener(final EventListener el) {
		if (el instanceof ChangeListener) {
			sizeSlider.addChangeListener((ChangeListener) el);
		}
		if (el instanceof ItemListener) {
			northRadioButton.addItemListener((ItemListener) el);
			westRadioButton.addItemListener((ItemListener) el);
			eastRadioButton.addItemListener((ItemListener) el);
			southRadioButton.addItemListener((ItemListener) el);

			alwaysontopJComboBox.addItemListener((ItemListener) el);
			languageJComboBox.addItemListener((ItemListener) el);
		}
		if (el instanceof ActionListener)
		{
			colorButton.addActionListener((ActionListener) el);
			resetButton.addActionListener((ActionListener) el);
		}
	}
}
